"""
RandCompBreakerPlayer.py

Author: Zach Epolito
Date: 9-21-08
"""

from WaitFrame import WaitFrame
from MoveCommand import MoveCommand
from Game import Game
from BreakerMove import BreakerMove
from random import randint
import time

"""represents a random guessing breaker"""
class RandCompBreakerPlayer:

    #Frame for getting Breaker input

    """Constructor."""
    def __init__(self, game, host, gui, waitTime):
        self.game = game
        self.host = host
        self.gui = gui
        self.waitTime = waitTime
        self.frame = None
        
        game.register_observer(self)

    """sends a random list of integers through
    a functor. Depreciated
    def strategy(self, contFunctor, GUI, waitTime):
        #Display waiting in GUI
        if self.frame is None:
            self.frame = WaitFrame(GUI.getRootInputView(), contFunctor, width=300, height=100)
        GUI.set_input_view(self.frame)
        self.frame.update()
        self.frame.update()
        # sleep
        time.sleep(waitTime)
        
        #Tells the GUI class that it should no
        #longer be waiting

        #Creates a random game move and
        #send it to the controller
        guess = [randint(0,5), randint(0,5), randint(0,5), randint(0,5)]
        contFunctor(guess)"""

    def update(self, subject, param):

        print "Breaker Turn Start"
        if subject == self.game:
            if param == self.game.NOTIFY_MOVE:
                if self.game.turn == self.game.BREAKER_TURN :
                    if self.frame == None:
                        self.frame = WaitFrame(self.gui.getRootInputView(),
                                                   width=300, height=100)
                    self.gui.set_input_view(self.frame)
                    self.frame.update()
                    self.frame.update()
                    # sleep
                    time.sleep(self.waitTime)

                    #Creates a random game move and
                    #send it to the controller
                    guess = [randint(0,5), randint(0,5), randint(0,5), randint(0,5)]
                    breakerMove = BreakerMove(guess)

                    self.moveCommand.execute(breakerMove)
                    #elif param == Game.NOTIFY_UNDO:
        print "Breaker Turn End"
        
    def makeFirstMove(self, game):
        if self.frame is None:
            self.frame = WaitFrame(self.gui.getRootInputView(), width=300, height=100)
        self.gui.set_input_view(self.frame)
        print "Breaker First Move"
        self.frame.update()
        self.frame.update()
        # sleep
        time.sleep(self.waitTime)

        #Creates a random game move and
        #send it to the controller
        guess = [randint(0,5), randint(0,5), randint(0,5), randint(0,5)]
        breakerMove = BreakerMove(guess)

        self.moveCommand.execute(breakerMove)
        #elif param == Game.NOTIFY_UNDO:

    def setWaitTime(self, waitTime):
        self.waitTime = waitTime

    def setCommands(self, undoCommand, winCommand, moveCommand,
                    exitCommand, newGameCommand):
        self.moveCommand = moveCommand

    """handle if new game or exit command is executed"""
    def handleCommandExecuted(self, cmd):
        pass
